<template>
  <div class="user-center">
    <div class="user-container">
      <h1 class="page-title">用户中心</h1>
      
      <!-- 已登录状态 -->
      <div v-if="userStore.isLoggedIn" class="user-profile">
        <div class="profile-card">
          <div class="avatar">
            <img :src="userStore.userInfo.avatar" :alt="userStore.userInfo.name" />
          </div>
          <div class="user-info">
            <h2>欢迎回来，{{ userStore.userInfo.name }}！</h2>
            <p>用户ID: {{ userStore.userInfo.id }}</p>
          </div>
        </div>
        
        <div class="user-actions">
          <div class="action-card">
            <h3>购物车</h3>
            <p>您的购物车中有 {{ cartStore.totalItems }} 件商品</p>
            <button class="action-btn" @click="goToCart">
              查看购物车
            </button>
          </div>
          
          <div class="action-card">
            <h3>个人设置</h3>
            <p>管理您的个人信息和偏好设置</p>
            <button class="action-btn" @click="goToSettings">
              编辑资料
            </button>
          </div>
        </div>
        
        <div class="logout-section">
          <button class="logout-btn" @click="logout">
            退出登录
          </button>
        </div>
      </div>
      
      <!-- 未登录状态 -->
      <div v-else class="login-section">
        <div class="login-card">
          <h2>登录到您的账户</h2>
          <form @submit.prevent="login" class="login-form">
            <div class="form-group">
              <label for="username">用户名</label>
              <input 
                type="text" 
                id="username" 
                v-model="loginForm.username"
                placeholder="请输入用户名"
                required
              />
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input 
                type="password" 
                id="password" 
                v-model="loginForm.password"
                placeholder="请输入密码"
                required
              />
            </div>
            <button type="submit" class="login-btn">
              登录
            </button>
          </form>
         <!--<p class="demo-tip">
            <strong>演示提示：</strong>输入任意用户名和密码即可登录
          </p>-->
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { useCartStore } from '../stores/cart'

const router = useRouter()
const userStore = useUserStore()
const cartStore = useCartStore()

const loginForm = ref({
  username: '',
  password: ''
})

const login = () => {
  // 模拟登录验证
  if (loginForm.value.username && loginForm.value.password) {
    userStore.login({
      id: Date.now(),
      name: loginForm.value.username,
      avatar: 'https://via.placeholder.com/100x100/007ACC/FFFFFF?text=' + loginForm.value.username.charAt(0).toUpperCase()
    })
    alert('登录成功！')
  }
}

const logout = () => {
  if (confirm('确定要退出登录吗？')) {
    userStore.logout()
    alert('已退出登录')
  }
}

const goToCart = () => {
  router.push('/cart')
}

const goToSettings = () => {
  router.push('/settings')
}
</script>

<style scoped>
.user-center {
  min-height: 100vh;
  background-color: #f8f9fa;
}

.user-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  text-align: center;
  margin-bottom: 40px;
  color: #333;
}

.profile-card {
  display: flex;
  align-items: center;
  background: white;
  border-radius: 12px;
  padding: 32px;
  margin-bottom: 32px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  gap: 24px;
}

.avatar {
  flex-shrink: 0;
}

.avatar img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #007acc;
}

.user-info h2 {
  font-size: 1.8rem;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
}

.user-info p {
  color: #666;
  font-size: 1rem;
}

.user-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 32px;
  margin-bottom: 32px;
  max-width: 800px;
}

.action-card {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.action-card h3 {
  font-size: 1.3rem;
  font-weight: bold;
  margin-bottom: 12px;
  color: #333;
}

.action-card p {
  color: #666;
  margin-bottom: 16px;
  line-height: 1.5;
}

.action-btn {
  padding: 10px 20px;
  background-color: #007acc;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  transition: background-color 0.2s ease;
}

.action-btn:hover {
  background-color: #005c99;
}

.logout-section {
  text-align: center;
}

.logout-btn {
  padding: 12px 32px;
  background-color: #dc3545;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.logout-btn:hover {
  background-color: #c82333;
}

.login-section {
  display: flex;
  justify-content: center;
}

.login-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-card h2 {
  font-size: 1.8rem;
  font-weight: bold;
  text-align: center;
  margin-bottom: 32px;
  color: #333;
}

.login-form {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 2px solid #e0e0e0;
  border-radius: 6px;
  font-size: 1rem;
  transition: border-color 0.2s ease;
}

.form-group input:focus {
  outline: none;
  border-color: #007acc;
}

.login-btn {
  width: 100%;
  padding: 14px;
  background-color: #007acc;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.login-btn:hover {
  background-color: #005c99;
}

.demo-tip {
  text-align: center;
  color: #666;
  font-size: 0.9rem;
  margin-top: 16px;
  padding: 12px;
  background-color: #f8f9fa;
  border-radius: 6px;
}

@media (max-width: 768px) {
  .profile-card {
    flex-direction: column;
    text-align: center;
  }
  
  .user-actions {
    grid-template-columns: 1fr;
  }
  
  .page-title {
    font-size: 2rem;
  }
}
</style> 